package net.lzzy.practices.web.admin;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.lzzy.practices.constants.ApiConstants;
import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.services.UserService;
import net.lzzy.practices.utils.AuthUtils;
import net.lzzy.practices.web.models.ApiResult;
import net.lzzy.practices.web.models.VmUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author liaodcc on 2022/5/16 10:00.
 */
@Api(tags = "用户相关业务")
@CrossOrigin
@RestController
@RequestMapping(ApiConstants.ROUTE_USER_ROOT)
public class UserApiController {
    private final UserService service;

    @Autowired
    public UserApiController(UserService service) {
        this.service = service;
    }
    @GetMapping(ApiConstants.ROUTE_USER_ALL)
    @ApiOperation(value = "获取所有用户",notes = "1.token(String)<br/>")
    public ApiResult getUsers(@RequestParam String token){
        List<VmUser> users = new ArrayList<>();
        service.get().forEach(user -> users.add(VmUser.create(user,true)));
        return new ApiResult(true,users.size() + "",users);
    }
    @GetMapping(ApiConstants.ROUTE_USER_PAGE)
    @ApiOperation(value = "获取所有用户",notes = "1.token(String)<br/>2.page(int)<br/>3.size(int)<br/>")
    public ApiResult getPagedUsers(@RequestParam int page, @RequestParam String token, @RequestParam int size){
        List<VmUser> users = new ArrayList<>();
        service.get(page,size).forEach(user -> users.add(VmUser.create(user,true)));
        return new ApiResult(true,service.count() + "",users);
    }
    @GetMapping(ApiConstants.ROUTE_USER_SEARCH)
    @ApiOperation(value = "查询用户",notes = "1.token(String)<br/>2.ke(String)<br/>")
    public ApiResult search(@RequestParam String token,@PathVariable String kw){
        List<VmUser> users = new ArrayList<>();
        service.search(kw).forEach(user -> users.add(VmUser.create(user,true)));
        return new ApiResult(true,users.size() + "",users);
    }

    @GetMapping(ApiConstants.ROUTE_USER_APPLYING)
    @ApiOperation(value = "审批正在申请的用户",notes = "1.token(String)<br/>")
    public ApiResult getApplyingUsers(@RequestParam String token){
        List<VmUser> users = new ArrayList<>();
        service.getApplying(true).forEach(user -> users.add(VmUser.create(user,true)));
        return new ApiResult(true,users.size() + "",users);
    }
    @PostMapping(ApiConstants.ROUTE_USER_APPROVE)
    @ApiOperation(value = "审批教师申请",notes = "1.token(String)<br/>2.id(UUID)<br/>")
    public ApiResult approve(@RequestParam String token, @RequestParam UUID id){
        User user = service.approveTeacher(id);
        if (user == null){
            return new ApiResult(false,Messages.WRONG_ID.toString(), null);
        }
        return new ApiResult(true,"",VmUser.create(user,true));
    }
    @PostMapping(ApiConstants.ROUTE_USER_DECLINE)
    @ApiOperation(value = "驳回审批教师申请",notes = "1.token(String)<br/>2.id(UUID)<br/>")
    public ApiResult decline(@RequestParam String token, @RequestParam UUID id){
        User user = service.declineTeacher(id);
        if (user == null){
            return new ApiResult(false,Messages.WRONG_ID.toString(), null);
        }
        return new ApiResult(true,"",VmUser.create(user,true));
    }
    @PostMapping(ApiConstants.ROUTE_USER_BAN)
    @ApiOperation(value = "禁止用户登录",notes = "1.token(String)<br/>2.id(UUID)<br/>")
    public ApiResult ban(@RequestParam String token, @RequestParam UUID id){
        User user = service.banUser(id);
        if (user == null){
            return new ApiResult(false,Messages.WRONG_ID.toString(), null);
        }
        return new ApiResult(true,"",VmUser.create(user,true));
    }
}
